#coding=utf-8
from uliweb import expose

#可以省略直接引用
from uliweb import request, error, redirect
from uliweb.form import *
from uliweb.orm import get_model

def __begin__():
    """
    用户验证 权限验证
    """
    from uliweb import functions
    return functions.require_login()
    #return functions.has_role()

@expose('/myrbacpost')
class PostData(object):

    def __init__(self):
        self.menurolemodel = get_model('menurole')
        self.menumodel = get_model('menu')
    
    def __begin__(self):
        if not request.user.is_superuser:
            error('你不是超级用户不能进行这项操作！')    


    def myrole(self):
        """        
        所有权限的数据
        """
        
        #获取登录用id
        user = request.user
    
        #得到偏移量
        pageIndex= request.GET.get('offset',0)
        #得到查询个数
        pageSize = request.GET.get('limit',20)
    
        #得到排序字段
        sort = request.GET.get('sort','id')
        #得到排序规则
        order_by_type = request.GET.get('order','asc')
    
        #使用sqlchemy自带asc方法，uliweb版本  self.menumodel.c['id'].asc()或self.menumodel.c.id.asc()
        if order_by_type == 'asc':
            #升序
            #order_by = asc('id')
            order_by = self.menurolemodel.c[sort].asc()
        else:
            #降序
            #order_by = desc ('id')
            order_by = self.menurolemodel.c[sort].desc()
    
        data = {}
        data['rows'] = []
    
    
        #得到总数
        data['total'] = self.menurolemodel.all().count()
    
        #分页获取数据
        ormdata = self.menurolemodel.all().order_by(order_by).limit(pageSize).offset(pageIndex)

        for i in ormdata:
            
            menuname = []

            usersname = []
            for j in i.menus:
                menuname.append(j.menuname)

                
            for k in i.users:
                usersname.append(k.nickname)        
            
            data['rows'].append({"id":i.id,
                                 "name":i.name,
                                 "description":i.description,
                                 "menuname":",".join(menuname),
                                 "usersname":",".join(usersname)
                                 })

        #返回json串
        return json(data)    
           
    
######################################################################################################

